package com.suppor.rpc.proto.handler;

import com.suppor.rpc.core.trans.RpcResponse;
import com.suppor.rpc.core.util.RpcFuture;
import com.suppor.rpc.core.util.RpcServiceUtil;
import com.suppor.rpc.proto.protocol.RpcProtocol;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class RpcResponseHandler extends SimpleChannelInboundHandler<RpcProtocol<RpcResponse>> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, RpcProtocol<RpcResponse> rpcResponseRpcProtocol) throws Exception {
        Long requestId = rpcResponseRpcProtocol.getHeader().getRequestId();
        RpcFuture<RpcResponse> future = RpcServiceUtil.responseFuture.remove(requestId);
        if (null != future) {
            // 异步set 结果
            future.getPromise().setSuccess(rpcResponseRpcProtocol.getBody());
        }
    }
}
